package com.weijian.advisor;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.ChatClientRequest;
import org.springframework.ai.chat.client.ChatClientResponse;
import org.springframework.ai.chat.client.advisor.api.CallAdvisor;
import org.springframework.ai.chat.client.advisor.api.CallAdvisorChain;
import org.springframework.ai.chat.client.advisor.api.StreamAdvisor;
import org.springframework.ai.chat.client.advisor.api.StreamAdvisorChain;
import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import reactor.core.publisher.Flux;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
public class RagLoggerAdvisor implements CallAdvisor, StreamAdvisor {

    private final VectorStore vectorStore;

    private final ChatClient chatClient;

    //获取当前 Advisor 的名称
    @Override
    public String getName() {
        return this.getClass().getSimpleName();
    }

    //执行顺序  越小越先执行
    @Override
    public int getOrder() {
        return 0;
    }

    //请求处理前的处理逻辑
    private ChatClientRequest before(ChatClientRequest request) {
        String content = chatClient.prompt("我需要做rag检索，请根据用户的问题归纳出检索的关键词，注意回答只有关键词，不要回复其他无关的内容").user(request.toString()).call().content();
        log.info("RAG检索关键词：{}", content);
//        String userQuery = request.userText();

        SearchRequest searchRequest = SearchRequest.builder()
                .similarityThreshold(0.50)
                .topK(3)
                .query(content)
                .build();
        // 使用 VectorStore 手动执行相似性搜索
        List<Document> retrievedDocs = vectorStore.similaritySearch(searchRequest);
        if (retrievedDocs.isEmpty()) {
            log.info("No relevant documents found.");
        }

        for (Document doc : retrievedDocs) {
            log.info(" - Document ID: {}", doc.getId());
            log.info("   Content Snippet: {}", doc.getMetadata().get("title"));
        }
        log.info("AI Request: {}", request.context());
        return request;
    }

    //请求处理后的处理逻辑
    private void observeAfter(ChatClientResponse ChatClientResponse) {
    }

    @Override
    public ChatClientResponse adviseCall(ChatClientRequest ChatClientRequest, CallAdvisorChain chain) {
        ChatClientRequest = this.before(ChatClientRequest);
        ChatClientResponse ChatClientResponse = chain.nextCall(ChatClientRequest);
        this.observeAfter(ChatClientResponse);
        return ChatClientResponse;
    }

    @NotNull
    @Override
    public Flux<ChatClientResponse> adviseStream(@NotNull ChatClientRequest ChatClientRequest, StreamAdvisorChain chain) {
        ChatClientRequest = this.before(ChatClientRequest);
        Flux<ChatClientResponse> chatClientResponseFlux = chain.nextStream(ChatClientRequest);
        return chatClientResponseFlux;
    }
}
